package com.anzia.storeback.service.impl;

import com.anzia.storeback.dao.ProductDao;
import com.anzia.storeback.entity.Category;
import com.anzia.storeback.entity.Product;
import com.anzia.storeback.entity.Product_picture;
import com.anzia.storeback.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *@ClassName ProductServiceImpl
 *@Description
 *@Author Anzia.
 *@Date 2021/5/31 16:17
 *@Version 1.0
 **/
@Service("productServiceImpl")
public class ProductServiceImpl  implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<Category> getCategory() {
        return productDao.getCategory();
    }

    @Override
    public List<Product> getAllProduct() {

        // 从redis中获取变量中的键值对
        Map<Object, Object> products = redisTemplate.opsForHash().entries("products");
        // 如果redis中无数据，查询数据库
        if (products == null || products.size() == 0) {
            // 调用数据库接口
            List<Product> allProduct = productDao.getAllProduct();
            // 将数据存到对象中
            products.put("product", allProduct);
            // 将对象中的数据存到redis中
            redisTemplate.opsForHash().putAll("product", products);
        }

        // 返回从redis或数据库中得到的数据
        return (List<Product>) products.get("product");
    }

    @Override
    public List getCategory_id() {

        // 从redis中获取该值下的键值对
        Map<Object, Object> category_id = redisTemplate.opsForHash().entries("category_id");
        // 如果redis中为空，则从数据库中获取
        if (category_id.isEmpty()) {
            List category_idList = productDao.getCategory_id();
            category_id.put("category_id", category_idList);
            // 获取完毕后再在redis中存一份
            redisTemplate.opsForHash().putAll("category_id", category_id);
        }

        return (List) category_id.get("category_id");
    }

    @Override
    public List<Product> getProductByCategory(int categoryID, int currentPage, int pageSize) {
        return productDao.getProductByCategory(categoryID, currentPage, pageSize);
    }

    @Override
    public List<Product> getProductByCategoryName(String categoryName) {
        return productDao.getProductByCategoryName(categoryName);
    }

    @Override
    public Product getProductByID(int product_id) {

        // 从redis中获取该值下的键值对
        Map<Object, Object> productMap = redisTemplate.opsForHash().entries("product");
        // redis中没有从数据库中获取
        if (productMap.isEmpty()) {
            Product product = productDao.getProductByID(product_id);
            productMap.put(product_id + "", product);
            redisTemplate.opsForHash().putAll("product", productMap);
        } else if (productMap.get(product_id) == null) { // 如果数据库或redis中没有该商品，根据id获取后存入对象中
            productMap.put(product_id + "", productDao.getProductByID(product_id));
            redisTemplate.opsForHash().putAll("product", productMap);
        }

        return (Product) productMap.get(product_id + "");
    }

    @Override
    public List<Product_picture> getDetailsPicture(int product_id) {
        return productDao.getDetailsPicture(product_id);
    }

    @Override
    public List<Product> getProductBySearch(String search) {
        return productDao.getProductBySearch(search);
    }
}
